<?php
namespace app\house\model;
use app\BaseModel;
use think\facade\Db;



class IndexCollect extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_house_collect';

    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'house_text'
    ];

    /**
     * @param $value
     * @param $data
     * 获取房源
     */
    public function getHouseTextAttr($value,$data){
        $house_model = new IndexHouse();
        $house       = $house_model->houseInfo(['id'=>$data['house_id']]);
        return $house;
    }

    /**
     * User: chenniang
     * Date: 2019-11-15 22:19
     * @param $value
     * @return string
     * descption:转数据
     */

    public function getTextAttr($value){
        return empty($value)?'':$value;
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房产预约列表
     */
    public function collectList($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_house b' ,'a.house_id = b.id')
            ->where($dis)
            ->field(['a.*' ,'b.id as house_id'])
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * 添加预约
     */
    public function collectAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['text']        = !empty($data['text'] )?$data['text'] :0;
        $res = $this->insert($data);
        return $res;
    }


    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 添加收藏
     */
    public function collectInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():$data;
    }


    /**
     * @param $dis
     * @return bool
     * @throws \Exception
     * 删除收藏
     */
    public function collectDel($dis){
        $res = $this->where($dis)->delete();
        return $res;

    }


    /**
     * @param $dis
     * @return int
     * 收藏数
     */
    public function collectCount($dis){
        $res = $this->where($dis)->count();
        return $res;

    }

    /**
     * User: chenniang
     * Date: 2019-11-01 14:03
     * @param $dis
     * @param $data
     * @return IndexCollect
     * descption:编辑收藏
     */
    public function collectUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }





}